package com.bwie.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.pojo.dto.CustomerDto;
import com.bwie.pojo.po.Dian;
import com.bwie.pojo.po.NameNum;
import com.bwie.pojo.vo.CustomerVo;
import com.bwie.service.CustomerService;
import com.bwie.service.DianService;
import com.bwie.utils.poi.EasyPoiUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private DianService dianService;
    @Autowired
    private EasyPoiUtil easyPoiUtil;

    /**
     * 动态下拉框
     * @param str
     * @return
     */
    @ResponseBody
    @GetMapping("getDianList")
    public List<Dian> getDianList(String str){
        List<Dian> list = dianService.getDianList(str);
        return list;
    }

    /**
     * 客户列表
     * @param customerDto
     * @return
     */
    @ResponseBody
    @GetMapping("getCustomerList")
    public PageInfo<CustomerVo> getCustomList(CustomerDto customerDto){
       PageInfo<CustomerVo> pageInfo =  customerService.getCustomList(customerDto);
       return pageInfo;
    }


    /**
     * 导出
     * @param
     * @return
     */
    @RequestMapping("daoChu")
    public void daoChu( HttpServletResponse response){
        CustomerDto customerDto = new CustomerDto();
        List<CustomerVo> list = customerService.getCustomList1(customerDto);
        easyPoiUtil.exportExcel(response,list,CustomerVo.class,"客户列表导出");

    }


    /**
     * 部分导出
     * @param response
     * @param customerDto
     */
    @RequestMapping("buFenDaoChu")
    public void buFenDaoChu(HttpServletResponse response,CustomerDto customerDto){
        List<CustomerVo> list = customerService.getCustomList1(customerDto);
        easyPoiUtil.exportExcel(response,list,CustomerVo.class,"客户列表条件导出");

    }







    /**
     * 级别图表
     * @param model
     * @return
     */
    @RequestMapping("getTable")
    public String  getTable(Model model){
        List<String> list = customerService.getCustomerLevel();
        List<Integer> list1 = customerService.getCustomerLevelNum();
        model.addAttribute("list", JSONObject.toJSONString(list));
        model.addAttribute("list1", JSONObject.toJSONString(list1));

        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        CustomerDto customerDto = new CustomerDto();
        List<NameNum> list2 = customerService.getCustomerLevelAndNum();


        for (NameNum nameNum : list2) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("value",nameNum.getNameNum());
            map1.put("name",nameNum.getDianValue());
            maps.add(map1);
        }

        model.addAttribute("maps",JSONObject.toJSONString(maps));
        return "/customer/level_demo";
    }




    @RequestMapping("getSourceDemo")
    public String getSourceDemo(Model model){
        List<NameNum> list = customerService.getCustomerSource();
        ArrayList<String> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        for (NameNum nameNum : list) {
            list1.add(nameNum.getDianValue());
            list2.add(nameNum.getNameNum());
        }

        model.addAttribute("class1",JSONObject.toJSONString(list1));
        model.addAttribute("class2",JSONObject.toJSONString(list2));

        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (NameNum nameNum : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("value",nameNum.getNameNum());
            map.put("name",nameNum.getDianValue());
            maps.add(map);
        }
        model.addAttribute("class3",JSONObject.toJSONString(maps));
        return "/customer/source_demo";
    }



}
